const Core = require('@alicloud/pop-core');
const rn = require("randomnumber");
const jwt = require("jsonwebtoken");

module.exports = {
    sendMsg(tel,code){
        var client = new Core({
            accessKeyId: 'LTAI4GHsvUyWBB9YBMfHbHk6',
            accessKeySecret: 'Om5GaHNITnjphhehT0lh4uL5ASnXJ2',
            endpoint: 'https://dysmsapi.aliyuncs.com',
            apiVersion: '2017-05-25'
        });
        var params = {
            "RegionId": "cn-hangzhou",
            "PhoneNumbers": tel,
            "SignName": "潜水的旱鸭子",
            "TemplateCode": "SMS_203728335",
            "TemplateParam": `{code:${code}}`
        }
        var requestOption = {
            method: 'POST'
        };
        return new Promise((resolve,reject)=>{
            client.request('SendSms', params, requestOption).then((result) => {
                console.log(JSON.stringify(result));
                resolve();
            }, (ex) => {
                console.log(ex);
                reject();
            })
        })
    },
    randomNum(n){
        return rn.generate(n);
    },
    verify(t){
        // let t = req.headers.token || req.body.token || req.query.token;
        return new Promise((resolve,reject)=>{
            jwt.verify(t,"千锋H5，牛！", (err,data)=>{
                if(err) reject();
                resolve(data);
            })
        })
    }
}
